package com.jyx.web.udp;

import java.net.*;
public class UDPClient {
	public static void main(String[] args) throws Exception{
		DatagramSocket ds=new DatagramSocket();//注册邮箱
		String text1="I am here!";
		byte[] bs1=text1.getBytes();//转换成字节用来传输
		DatagramPacket letter1=new DatagramPacket(
				bs1,0,bs1.length,InetAddress.getLocalHost(),9000);
		//写好信（地址和内容）
		ds.send(letter1);//发信	
		DatagramPacket letter2=new DatagramPacket(
				new byte[100],0,100);
		//准备空信用来接受服务端的信
		ds.receive(letter2);//收信
		byte[] data=letter2.getData();//得到信的内容
		int offset=letter2.getOffset();
		int length=letter2.getLength();
		String str=new String(data,offset,length);//转换成字符串读取	
		System.out.println(str);
		ds.close();
	}
}
